package com.mall507.fivepeaks.activity.community;

import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.Message;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * 有问必答详情
 * @date 2019/8/8
 */
@Route(path = "/community/CommunityProblemInfoRUI")
public class CommunityProblemInfoRUI extends AbsRecyclerViewUI<Message> {

    List<String> listUrl;
    View headView;

    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_community_problem_info;
    }

    @Override
    protected void bindView() {
        getTitleView().setContent("详情");
        headView = getLayoutInflater().inflate(R.layout.head_rv_community_problem_info, null);
        getAdapter().setHeaderView(headView);
        getComment();
    }

    @Override
    protected String setHttpPath() {
        return "Comment/getQuestionComment";
    }

    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected List<Message> handleHttpData(JSONObject response) {
        List<Message> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray jsonArray = response.optJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.optJSONObject(i);
                JSONObject member = jsonArray.optJSONObject(i).optJSONObject("member");
                Message model = new Message();
                model.setNid(object.optString("id"));
                if (member != null && member.length()>0){
                    model.setHead(member.optString("head_img"));
                    model.setName(member.optString("nickname"));
                }
                model.setDesc(object.optString("content"));
                model.setTime(object.optString("create_at"));
                list.add(model);
            }
        }
        return list;
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_comn_goods_item_info;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, Message model) {
        holder.setText(R.id.name, model.getName());
        holder.setText(R.id.desc, model.getDesc());
        holder.setText(R.id.time, model.getTime());
        ((CircleImageView) holder.getView(R.id.icon)).setImageURI(model.getHead());
    }

    private void getComment() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("Community/getQuestion", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                JSONObject data = response.optJSONObject("data");
                JSONObject member = data.optJSONObject("member");
                if (member != null && member.length()>0){
                    getTitleView().setContent(member.optString("nickname"));
                }
                ((TextView)headView.findViewById(R.id.head_title)).setText(data.optString("title")); //文章标题
                ((TextView)headView.findViewById(R.id.head_content)).setText(data.optString("content")); //文章内容
                ((TextView)headView.findViewById(R.id.head_pageview)).setText(data.optString("views")); //浏览量
                ((TextView)headView.findViewById(R.id.head_time)).setText(data.optString("create_at")); //文章发布时间
            }
        });
    }
}
